package com.seabook.store.purchase.web;

import java.util.Iterator;


import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.seabook.store.auth.web.UserSession;
import com.seabook.store.goodmgr.domain.Product;
import com.seabook.store.goodmgr.service.ProductService;
import com.seabook.store.purchase.domain.Cart;
import com.seabook.store.purchase.domain.CartItem;
import com.seabook.store.purchase.domain.Item;
import com.seabook.store.util.ServiceLocator;

@Controller
public class CartController {
    private final ProductService productService;

    public CartController() {
        this.productService = (ProductService) ServiceLocator
                .getBean("productService");
    }

    @RequestMapping(value = "/addToCart", method = RequestMethod.GET)
    public String addProductToCart(@RequestParam("productId") int productId,
            @RequestParam("quantity") int quantity,
            HttpServletRequest request) {
        Cart cart = UserSession.getUserSession(request).getCart();
        if (cart.containsItemId("" + productId)) {
            cart.incrementQuantityByItemId( "" + productId, quantity);
        }
        else {
            Product product = productService.getProductById(productId);
            Item item = new Item();
            item.setProduct(product);
            item.setQuantity(quantity);
            item.setItemId("" +productId);
            cart.addItem(item, true, quantity);
        }
        request.setAttribute("cart", cart);
        return "cart";
    }
    
    @RequestMapping(value = "/viewCart", method = RequestMethod.GET)
    public String viewCart(HttpServletRequest request) {
        Cart cart = UserSession.getUserSession(request).getCart();
        request.setAttribute("cart", cart);
        return "cart";
    }
    
    @RequestMapping(value = "/updateCart")
    public String updateCart(HttpServletRequest request) throws Exception {
        Cart cart = UserSession.getUserSession(request).getCart();
        Iterator<CartItem> cartItems = cart.getAllCartItems();
        while (cartItems.hasNext()) {
            CartItem cartItem = (CartItem) cartItems.next();
            String itemId = cartItem.getItem().getItemId();
            try {
                int quantity = Integer.parseInt(request.getParameter(itemId));
                cart.setQuantityByItemId(itemId, quantity);
                if (quantity < 1) {
                    cartItems.remove();
                }
            }
            catch (NumberFormatException ex) {
                // ignore on purpose
            }
        }
        request.setAttribute("cart", cart);
        return "cart";
    }
    
    @RequestMapping(value = "/removeItem")
    public String removeItem(@RequestParam("itemId") int productId, HttpServletRequest request) throws Exception {
        Cart cart = UserSession.getUserSession(request).getCart();
        cart.removeItemById("" + productId);
        request.setAttribute("cart", cart);
        return "cart";
    }
}
